#include "Minus.h"

long double Minus::calcul(long double aux1, long double aux2)
{
    return aux1 - aux2;
}

long double Minus::calcul()
{
    if (!fStang || !fDrept)
        exit(1);
    return calcul(fStang->calcul(), fDrept->calcul());
}

char *Minus::derivare()
{
    char *aux1, *aux2, *aux;

    if (!fStang || !fDrept)
        exit(1);

    aux1 = fStang->derivare();
    aux2 = fDrept->derivare();

    aux = concatTermeni(aux1, aux2);

    delete aux1; delete aux2;
    return aux;
}

long double Minus::calculDerivata()
{
    if (!fStang || !fDrept)
        exit(1);

    return calcul(fStang->calculDerivata(), fDrept->calculDerivata());
}

char *Minus::toString()
{
    char *aux, *aux1, *aux2;

    aux1 = fStang->toString();
    aux2 = fDrept->toString();

    aux = concatTermeni(aux1, aux2);
    delete aux1; delete aux2;

    return aux;
}

char *Minus::concatTermeni(char *aux1, char *aux2)
{
    char *aux;

    if (!strcmp(aux1, "0") && !strcmp(aux2, "0"))
        aux = cpystr(aux1);
    else if (!strcmp(aux1, "0"))
    {
        aux = new char[strlen(aux2) + 2];
        strcpy(aux, "-"); strcat(aux, aux2);
    }
    else if (!strcmp(aux2, "0")) aux = cpystr(aux1);
    else
    {
        aux = new char[strlen(aux1) + strlen(aux2) + 4];
        strcpy(aux, "(");
        strcat(aux, aux1); strcat(aux, "-"); strcat(aux, aux2); strcat(aux, ")");
    }
    return aux;
}
